// pages/fenlei/fenlei.js
var app = getApp()
const cai = wx.cloud.database().collection("cai")
const user = wx.cloud.database().collection("user")
var options_
Page({
  /**
   * 页面的初始数据
   */
  data: {
    flag: 0,
    currentTab: 0,
    shahe1: {},
    shahe2: {},
    shahe3: {},
    heyi: {},
    xueer: {},
    xinbei: {},
    qita: {},
    searchdata: '',
    ans: {},
    showModalStatus: false,
    animationData: {}
  },
  switchNav: function (e) {
    var page = this;
    var id = e.target.id;
    if (this.data.currentTab == id) {
      return false;
    } else {
      page.setData({
        currentTab: id
      });
    }
    page.setData({
      flag: id
    });
  },
  catchTouchMove: function (res) {
    return false
  },
  gotodetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.index._id,
    })
  },
  search(res) {
    this.setData({
      searchdata: res.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options_ = options
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 1
      },
      success: (res) => {
        this.setData({
          shahe1: res.result.data
        })
        //console.log(this.data.shahe1)
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 2
      },
      success: (res) => {
        this.setData({
          shahe2: res.result.data
        })
        //console.log(this.data.shahe2)
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 3
      },
      success: (res) => {
        this.setData({
          shahe3: res.result.data
        })
        // console.log(this.data.shahe3)
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 4
      },
      success: (res) => {
        this.setData({
          heyi: res.result.data
        })
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 5
      },
      success: (res) => {
        this.setData({
          xueer: res.result.data
        })
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 6
      },
      success: (res) => {
        this.setData({
          xinbei: res.result.data
        })
        //console.log(this.data.xinbei)
      }
    })
    wx.cloud.callFunction({
      name: "getdatabase2",
      data: {
        id: 7
      },
      success: (res) => {
        this.setData({
          qita: res.result.data
        })
        //console.log(this.data.qita)
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad(options_)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  clickme: function () {
    if(this.data.searchdata.length==0)
    {
      wx.showToast({
        title: "请输入搜索内容",
        icon: "error"
      })
      return
    }
    this.gosearch()
    this.showModal();
  },

  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(700).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(700).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  gosearch(res) {
    wx.cloud.callFunction({
      name:"search",
      data:{
        str:this.data.searchdata
      },
      success:(res)=>{
        console.log(res.result)
        this.setData({
          ans: res.result
        })
      }
    })
  },
})